package com.sxx.beautifulcity.adapter;

import android.support.v4.app.FragmentManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sxx.beautifulcity.R;
import com.sxx.beautifulcity.entity.AroundData;
import com.sxx.beautifulcity.entity.greendao.AroundDaoEntity;
import com.sxx.beautifulcity.listenerInterface.OnMyItemClickListener;

import java.util.List;

/**
 * Created by dllo on 15/10/26.
 */
public class AroundAdapter extends RecyclerView.Adapter<AroundAdapter.LineAdapterViewHolder>{
    private List<AroundDaoEntity> list;
    private OnMyItemClickListener listener;

    public AroundAdapter(FragmentManager supportFragmentManager) {
    }

    public void addData(List<AroundDaoEntity> list){
        this.list = list;
        notifyDataSetChanged();
    }


    public  void  setOnMyItemClickListener(OnMyItemClickListener l){
        listener = l;
    }
    /***
     * 内部类初始化控件
     */
    class LineAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView line_name;
        private LinearLayout line_linearLayout;
        private int position;

        public LineAdapterViewHolder(View itemView) {
            super(itemView);
            line_name = (TextView) itemView.findViewById(R.id.line_name);
            line_linearLayout = (LinearLayout) itemView.findViewById(R.id.line_linearLayout);
            line_linearLayout.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (list != null && list.size() > 0) {
                AroundDaoEntity data = list.get(position);
                if (listener != null) {
                    listener.onMyItemClick(data);
                }
            }
        }
    }


    /***
     * 加载布局
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public LineAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.activity_around_itme, null);

        return new LineAdapterViewHolder(view);
    }

    /****
     * 绑定数据
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(LineAdapterViewHolder holder, int position) {
        if (list != null && list.size() > 0) {
            AroundDaoEntity data = list.get(position);
            holder.line_name.setText(data.getName());
        }
        holder.position = position;
    }

    /***
     * 返回集合的行数
     * @return
     */
    @Override
    public int getItemCount() {
        return list != null && list.size() > 0 ? list.size() : 0;
    }


}
